package com.scsystem.xysc.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scsystem.xysc.mapper.ProductMapper;
import com.scsystem.xysc.mapper.UserMapper;
import com.scsystem.xysc.pojo.Cart;
import com.scsystem.xysc.pojo.Product;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.CartService;
import com.scsystem.xysc.mapper.CartMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author sky
* @description 针对表【cart】的数据库操作Service实现
* @createDate 2023-09-16 15:56:38
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements CartService{

    @Resource
    private CartMapper cartMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public ReturnResult<Boolean> addCart(Long uid, Long pid, Integer quantity) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid)
                .eq("product_id", pid);
        Cart selectOne = cartMapper.selectOne(queryWrapper);
        if (selectOne == null) {
            Product product = productMapper.selectById(pid);
            if (product != null) {
                BigDecimal multiply = product.getPrice().multiply(new BigDecimal(quantity));

                Cart cart = new Cart();
                cart.setUserId(uid);
                cart.setProductId(pid);
                cart.setQuantity(quantity);
                cart.setProductPrice(multiply);

                int insert = cartMapper.insert(cart);
                return insert == 1? new ReturnResult<Boolean>().success("添加成功", true) : new ReturnResult<Boolean>().fail("添加失败", false);
            }else {
                return new ReturnResult<Boolean>().fail("商品不存在", false);
            }
        }else {
            return updateCart(uid, pid, selectOne.getQuantity() + quantity);
        }
    }

    @Override
    public ReturnResult<Boolean> deleteCart(Long uid, Long pid) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid)
                        .eq("product_id", pid);
        int delete = cartMapper.delete(queryWrapper);
        return delete == 1 ? new ReturnResult<Boolean>().success("删除成功", true) : new ReturnResult<Boolean>().fail("删除失败", false);
    }

    @Override
    public ReturnResult<Boolean> updateCart(Long uid, Long pid, Integer quantity) {
        if (quantity > 0) {
            QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", uid)
                           .eq("product_id", pid);
            Cart cart = cartMapper.selectOne(queryWrapper);
            if (cart!= null) {
                Product product = productMapper.selectById(pid);
                BigDecimal multiply = product.getPrice().multiply(new BigDecimal(quantity));
                cart.setQuantity(quantity);
                cart.setProductPrice(multiply);
                int update = cartMapper.updateById(cart);
                return update == 1? new ReturnResult<Boolean>().success("修改成功", true) : new ReturnResult<Boolean>().fail("修改失败", false);
            }else {
                return new ReturnResult<Boolean>().fail("购物车中没有该商品", false);
            }
        }else {
            return new ReturnResult<Boolean>().fail("数量不能为0", false);
        }
    }

    @Override
    public ReturnResult<List<Cart>> selectCart(Long uid) {
        if (uid!= null) {
            QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", uid);
            List<Cart> carts = cartMapper.selectList(queryWrapper);
            if (!carts.isEmpty()) {
                return new ReturnResult<List<Cart>>().success("查询成功", carts);
            }else {
                return new ReturnResult<List<Cart>>().fail("查询失败，购物车无数据", carts);
            }
        }else {
            return new ReturnResult<List<Cart>>().fail("用户id不能为空", null);
        }
    }
}